package com.tocea.formation.connection;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class ConnectionManager {
	private static ConnectionManager connectionManagerObject;
	private static Connection connection;
	public 	static Statement statement;
	

	private ConnectionManager() {
		try {
			Class.forName("com.mysql.jdbc.Driver").newInstance();
			connection = DriverManager.getConnection("jdbc:mysql://localhost/library", "root", "");
			statement = connection.createStatement();		
		} catch (InstantiationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	
	}
	
	public static ConnectionManager getConnectionManager() {
		if (connectionManagerObject == null) {
			connectionManagerObject = new ConnectionManager();
		}
		return connectionManagerObject;
	}
	
	public static ResultSet executeQuery(String query){
		try {
			return statement.executeQuery(query);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public static Integer executeUpdate(String query){
		try {
			return statement.executeUpdate(query);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}
}
